﻿<cfsilent>
	<cfscript>
		
		event.setArg("pageTitle", "选课管理 - 预置学生课表");

		termId = event.getArg("TID");

		academicYear = event.getArg("AcademicYear");
		academicTerm = event.getArg("AcademicTerm");
		
		rs_subject = queryNew("grade");
		
		targetArgs = structNew();
		
		if ( len(termId) ) {
			
			structInsert(targetArgs, "TID", termId, true);
			
			/**
			*原系统，根据培养方案来
			*EXISTS(
				 SELECT 1 FROM t_plan p WHERE clz.cls_id = p.cls_id AND p.term_id = :termId AND p.cidx != 'XXX' AND p.cidx != '000'
			 )
			**/
		
			
			/* 读取选课学年的授课专业、年级、学院信息 */
			sql = "	SELECT
						DISTINCT clz.grade,i.institute_id,i.institute_name
					FROM
						t_class clz 
						INNER JOIN t_subject sbj ON clz.sbj_id = sbj.sbj_id 
						INNER JOIN t_institute i ON i.institute_id = sbj.institute_id 
						INNER JOIN t_term a ON a.year_term = clz.grade 
					WHERE
						clz.cls_scale > 0
						AND
						a.term_id = :termId 						
					ORDER BY
						clz.grade DESC,i.institute_name";
			
			queryObj = new Query(datasource = application.dnsSlave);
			queryObj.addParam( name="termId", value=termId, cfsqltype="cf_sql_char");
			rs_subject = queryObj.execute(sql = sql).getResult();
			
			gradeArray = [];
			insArray = [];

			for( a = 1; a le rs_subject.recordCount; a++ ){
				if (not arrayFind(gradeArray, rs_subject["grade"][a])) { arrayAppend(gradeArray, rs_subject["grade"][a]); }
				if (not arrayFind(insArray, [rs_subject["institute_id"][a],rs_subject["institute_name"][a]])) { arrayAppend(insArray, [rs_subject["institute_id"][a],rs_subject["institute_name"][a]]); }
			}

			currentTab = event.getArg("TabID", "labRequired");
		
			reqDepId = event.getArg("reqInstitute");
			reqGrade = event.getArg("reqGrade");
			reqLocked = event.getArg("reqLocked", "0");
			reqPrePaid = event.getArg("reqPrePaid", "0");
		
			rs_reqSubject = queryNew("grade");
		
			if ( len(reqDepId) and len(reqGrade) ) {
				/* 预置必修课专业目录 */
			
				if ( reqDepId eq "all" ) {
					/* 所有学院 */
					if ( reqGrade eq "all" ) {
						/* 所有年级 */
						
						writeLOg( text="all" );
						
						/**
						*根据培养方案来预置必修课
						AND 
					     EXISTS(
						    SELECT 1 FROM t_plan b WHERE a.cls_id = b.cls_id AND b.term_id = :termId AND b.cidx != 'XXX' AND b.cidx != '000'
					     )
						*
						*/
						
						sql = "SELECT
									a.grade,sbj.sbj_id,sbj.sbj_name,i.institute_name,COUNT(*) r_count,SUM(a.cls_scale) scale_sum
								FROM
									t_class a 
									INNER JOIN t_subject sbj ON sbj.sbj_id = a.sbj_id 
									INNER JOIN t_institute i ON i.institute_id = sbj.institute_id 
									INNER JOIN t_term b ON b.year_term = a.grade 
								WHERE
									a.cls_scale > 0 
									AND
									b.term_id = :termId 
								GROUP BY
									a.grade,i.institute_name,sbj.sbj_id,sbj.sbj_name
								ORDER BY
									a.grade DESC,i.institute_name,sbj.sbj_name";
						
						queryObj = new Query(datasource = application.dnsSlave);
						
						queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
						
						rs_reqSubject = queryObj.execute(sql = sql).getResult();
						
						writeDump( rs_reqSubject );
						
					}
					else {
						/* 指定年级 */
						sql = "	SELECT
									a.grade,sbj.sbj_id,sbj.sbj_name,i.institute_name,COUNT(*) r_count,SUM(a.cls_scale) scale_sum
								FROM
									t_class a INNER JOIN t_subject sbj ON sbj.sbj_id = a.sbj_id INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
								WHERE
									a.grade = :grade AND
									a.cls_scale > 0 AND EXISTS(
										SELECT 1 FROM t_plan b WHERE a.cls_id = b.cls_id AND b.term_id = :termId AND b.cidx != 'XXX' AND b.cidx != '000'
									)
								GROUP BY
									a.grade,i.institute_name,sbj.sbj_id,sbj.sbj_name
								ORDER BY
									a.grade DESC,i.institute_name,sbj.sbj_name";
						
						queryObj = new Query(datasource = application.dnsSlave);
						
						queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
						
						queryObj.addParam(name = "grade", value = reqGrade, cfsqltype = "cf_sql_char");
						rs_reqSubject = queryObj.execute(sql = sql).getResult();
					}
				} else {
					/* 指定学院 */
					if ( reqGrade eq "all" ) {
						/* 所有年级 */
						sql = "	SELECT
									a.grade,sbj.sbj_id,sbj.sbj_name,i.institute_name,COUNT(*) r_count,SUM(a.cls_scale) scale_sum
								FROM
									t_class a INNER JOIN t_subject sbj ON sbj.sbj_id = a.sbj_id INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
								WHERE
									i.institute_id = :depId AND
									a.cls_scale > 0 AND EXISTS(
										SELECT 1 FROM t_plan b WHERE a.cls_id = b.cls_id AND b.term_id = :termId AND b.cidx != 'XXX' AND b.cidx != '000'
									)
								GROUP BY
									a.grade,i.institute_name,sbj.sbj_id,sbj.sbj_name
								ORDER BY
									a.grade DESC,i.institute_name,sbj.sbj_name";
									
						queryObj = new Query(datasource = application.dnsSlave);
						queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "depId", value = reqDepId, cfsqltype = "cf_sql_varchar");
						rs_reqSubject = queryObj.execute(sql = sql).getResult();
						
					} else {
						/* 指定年级 */
						sql = "	SELECT
									a.grade,sbj.sbj_id,sbj.sbj_name,i.institute_name,COUNT(*) r_count,SUM(a.cls_scale) scale_sum
								FROM
									t_class a INNER JOIN t_subject sbj ON sbj.sbj_id = a.sbj_id INNER JOIN t_institute i ON i.institute_id = sbj.institute_id
								WHERE
									i.institute_id = :depId AND a.grade = :grade AND
									a.cls_scale > 0 AND EXISTS(
										SELECT 1 FROM t_plan b WHERE a.cls_id = b.cls_id AND b.term_id = :termId AND b.cidx != 'XXX' AND b.cidx != '000'
									)
								GROUP BY
									a.grade,i.institute_name,sbj.sbj_id,sbj.sbj_name
								ORDER BY
									a.grade DESC,i.institute_name,sbj.sbj_name";
									
						queryObj = new Query(datasource = application.dnsSlave);
						queryObj.addParam(name = "termId", value = termId, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "grade", value = reqGrade, cfsqltype = "cf_sql_char");
						queryObj.addParam(name = "depId", value = reqDepId, cfsqltype = "cf_sql_varchar");
						rs_reqSubject = queryObj.execute(sql = sql).getResult();
					}
				}

			}
		
			newLocation = replace( buildURL( "taskClass", targetArgs ), "&amp;", "&", "all");
		
		}
		
	</cfscript>
</cfsilent>

<div id="headArea">
	<div class="uiHeader uiHeaderWithImage ptm">
		<div class="clearfix uiHeaderTop">
			<div class="uiHeaderActions rfloat">
				<a class="uiButton" href="<cfoutput>#buildURL('taskClass', targetArgs)#</cfoutput>"><i class="mrs img btnPublish"></i><span class="uiButtonText">教学任务</span></a>
			</div>
			<div>
				<h2 class="uiHeaderTitle">
					<i class="uiHeaderImage img icon16x16 card"></i><cfif len(termId)><cfoutput>#academicYear#</cfoutput>-<cfoutput>#academicTerm#</cfoutput>学期</cfif>预置学生课表
				</h2>
			</div>
		</div>
	</div>
</div>

<div class="UIElement">
	<ul class="senateFirstPage">
		<li>
			<div class="clearfix" id="create">
				
				<cfif termId eq "">
					<!-- termSelector -->
					<cfoutput>#event.getArg("termSelector")#</cfoutput>
					<!-- /termSelector -->
				</cfif>
				
				<cfif len(termId)>

					<div class="UItab">
						<div class="tabLables">
							<span <cfif currentTab eq "labRequired">class="active"</cfif> id="labRequired" tabTarget="Required">预置必修课</span>
							<span <cfif currentTab eq "labOptional">class="active"</cfif> id="labOptional" tabTarget="Optional">选修课学生名单预置</span>
						</div>
					</div>
					<script language="javascript" type="text/javascript">
						//<![CDATA[
						<!--
						$(document).ready(function() {
						
							/* init Tab */
							$("div.tabContent").hide();
							$("div#" + $("div.tabLables span.active").attr("tabTarget")).show();
						
							$("div.tabLables > span").click(function(){
								var $targetID = $(this).attr("tabTarget");
							
								$("div.tabLables span").removeClass("active");
								$(this).addClass("active");
							
								$("div.tabContent").not('[id="' + $targetID + '"]').hide();
								$("div.tabContent#" + $targetID).show();
							});
						
						});
					
						// -->
						//]]>
					</script>
				
					<div id="Required" class="tabContent">
					
						<div class="systemNotice">
							<h3><span class="img icon16x16 info"></span>请仔细核对</h3>
							<p>请慎重选择要预置必修课程的学院和年级, 系统将根据您选择的学生范围和培养方案要求, 自动生成必修课选课记录.</p>
						</div>
					
						<form id="formRequired" class="formWrapper <cfif rs_reqSubject.recordCount>hidden_elem</cfif>" method="post" action="<cfoutput>#buildURL('setStudentCourse', targetArgs)#</cfoutput>">
						
							<input name="TabID" type="hidden" value="labRequired" />

							<div class="label">
								<b>学生所在学院</b>
								<select name="reqInstitute" id="reqInstitute">
									<option value="all">全部</option>
									<cfloop array="#insArray#" index="ins">
										<option <cfif reqDepId eq ins[1]>selected="selected"</cfif> value="<cfoutput>#ins[1]#</cfoutput>"><cfoutput>#ins[2]#</cfoutput></option>
									</cfloop>
								</select>
							</div>
							<div class="label">
								<b>年级</b>
								<select name="reqGrade" id="reqGrade">
									<option value="all">全部</option>
									<cfloop array="#gradeArray#" index="grade">
										<option <cfif reqGrade eq grade>selected="selected"</cfif> value="<cfoutput>#grade#</cfoutput>"><cfoutput>#grade#</cfoutput>级</option>
									</cfloop>
								</select>
							</div>
						
							<hr/>
							<div class="label">
								<b>选项</b>
								<input name="reqLocked" id="reqLocked" type="checkbox" checked="checked" class="typeCheckbox" value="1" />禁止学生退选
								&nbsp;&nbsp;
								<input name="reqPrePaid" id="reqPrePaid" type="checkbox" checked="checked" class="typeCheckbox" value="1" />从预交学费中扣除课程学费
							</div>
							<hr/>
						
							<div class="lable">
								<input class="button1" type="submit" value="确认年级、学院" />
								<input class="button" type="reset" value="重设表格" />
							</div>
						</form>
					
						<cfif rs_reqSubject.recordCount>
						
							<form id="formRequiredOp" class="formWrapper Transparent" method="post" action="<cfoutput>#buildURL('setStudentCourse')#</cfoutput>">
						
								<table id="reqList" class="UIEditable">
									<thead>
										<tr>
											<td>学院</td>
											<td>年级</td>
											<td>专业</td>
											<td>班级数量</td>
											<td>在校学生总数</td>
											<td width="40"></td>
										</tr>
									</thead>
									<tbody>
                                        <cfloop query="rs_reqSubject">
											<cfset rowid = rs_reqSubject.grade & "," & rs_reqSubject.sbj_id />
											<tr class="editRows" rowid="<cfoutput>#rowid#</cfoutput>">
												<td><cfoutput>#rs_reqSubject.institute_name#</cfoutput></td>
												<td><span class="numeric"><cfoutput>#rs_reqSubject.grade#</cfoutput></span></td>
												<td><cfoutput>#rs_reqSubject.sbj_name#</cfoutput></td>
												<td><span class="numeric"><cfoutput>#rs_reqSubject.r_count#</cfoutput></span></td>
												<td><span class="numeric"><cfoutput>#rs_reqSubject.scale_sum#</cfoutput></span></td>
												<td><span class="iconProg L1"></span></td>
											</tr>
										</cfloop>
									</tbody>
								</table>

								<div class="lable">
									<input onclick="reqSet(0);" class="button1" type="button" value="预置必修课表" />
									<input onclick="reqReset();" class="button" type="button" value="返回" />
								</div>
						
							</form>
							<div id="out"></div>
						
							<cfset row = 0 />
						
							<script language="javascript" type="text/javascript">
								//<![CDATA[
								<!--

								var reqArray = [<cfloop query="rs_reqSubject"><cfset row++ />['<cfoutput>#rs_reqSubject.grade#</cfoutput>','<cfoutput>#rs_reqSubject.sbj_id#</cfoutput>']<cfif row lt rs_reqSubject.recordCount>,</cfif></cfloop>];

								function reqSet(step) {
								
									if ( step <= reqArray.length ) {
									
										var grade = reqArray[step][0];
										var subject = reqArray[step][1];
									
										var rowid = grade + "," + subject;
										var $tr = $("tr[rowid='" + rowid + "']", $("#reqList"));
									
										/* 处理第一项时, 使用阻挡提示 */
										if (step == 0) {
									
											$.blockUI({ 
														message: '正在预置学生课表...',
														css: { 
															'width': '16%',
															'left': '42%',
															'border': 'none', 
															'padding': '15px 5px', 
															'backgroundColor': '#000', 
															'border-radius': '4px', 
															'opacity': .8, 
															'color': '#fff',
															'cursor': null
															},
														overlayCSS: {
															'backgroundColor': '#fff', 
															'opacity': .1 ,
															'cursor': null
															}, 
														}); 
										}
									
										$.post(
												"<cfoutput>#buildURL('apiPreCourseRequired')#</cfoutput>", 
												{
													'Grade' : grade,
													'SubjectID' : subject,
													'TermID' : '<cfoutput>#termId#</cfoutput>',
													'Locked' : '<cfoutput>#reqLocked#</cfoutput>',
													'PrePaid' : '<cfoutput>#reqPrePaid#</cfoutput>',
													'seed': (new Date()).getTime()
												}, 
												function(data) {
					
													//$("#out").html(data);
													result = $.parseJSON(data);

													if ( result.success ) {

														/* 刷新处理进程数据 */
														$("span.iconProg", $tr).removeClass("L1").addClass("L5");
													
														/* 如果未处理完 跳到下一条 */
														step++;

														if ( step < reqArray.length ) {
															reqSet(step);
														}
														else {
															/* 取消阻挡 */
															$.unblockUI();
														
															alert("完成课表预置");
														
															window.location = '<cfoutput>#newLocation#</cfoutput>';
														
														}
														
													}
													else {
														/* 取消阻挡 */
														$.unblockUI();

														alert("预置课表失败");
													}
												
												});
									
									}
								
								}
							
								function reqReset() {
								
									$("#formRequiredOp").addClass("hidden_elem");
									$("#formRequired").removeClass("hidden_elem");
								
								}
							
								// -->
								//]]>
							</script>
						
						</cfif>
					
					</div>
				
					<div id="Optional" class="tabContent">
					</div>
				
				</cfif>
			</div>
		</li>
	</ul>
</div>
				
